package com.zhihu_lvliangi_android.option.zhengwu;

import java.util.ArrayList;
import java.util.List;

import com.android.volley.VolleyError;
import com.android.widget.listview.pullload.PullLoadListView;
import com.android.widget.listview.pullload.PullLoadListView.OnRefreshListener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhihu_lvliangi_android.R;
import com.zhihu_lvliangi_android.R.id;
import com.zhihu_lvliangi_android.R.layout;
import com.zhihu_lvliangi_android.R.menu;
import com.zhihu_lvliangi_android.domain.JsonPageModel;
import com.zhihu_lvliangi_android.domain.NewItem;
import com.zhihu_lvliangi_android.domain.ZhengwuColumnItem;
import com.zhihu_lvliangi_android.option.general.GeneralWebviewActivity;
import com.zhihu_lvliangi_android.option.parent.ParentActivity;
import com.zhihu_lvliangi_android.option.zhengwu.adapter.NewsListViewAdapter;
import com.zhihu_lvliangi_android.service.DataFormat;
import com.zhihu_lvliangi_android.service.MobileApplication;
import com.zhihu_lvliangi_android.service.TaskID;
import com.zhihu_lvliangi_android.util.RequestURL;

import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class NotificationActivity extends ParentActivity {

	private List<NewItem> mNewsList = new ArrayList<NewItem>();;
	private PullLoadListView mPullListView;
	private NewsListViewAdapter adapter;

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == android.R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void initActionBar() {
		ActionBar actionBar = getSupportActionBar();
		actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar));
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setIcon(new BitmapDrawable());//不设置图标icon
	}

	@Override
	protected int getLayoutId() {
		return R.layout.activity_notification;
	}

	@Override
	protected void setupViews() {
		initActionBar();
		/*NewItem item = new NewItem();
		item.setNews_type_name("吕梁市公安局");
		item.setNews_title("省政协领导来我市政务大厅进行调研检查");
		item.setRelease_time("2014//11/16");
		item.setRelease_people("浏览 2154 次");
		item.setNews_content_html("http://192.168.1.204:8080/llwif/service/news/E90F089B35224737B9B73654C2A4C5D7.html");
		mNewsList.add(item);
		
		item = new NewItem();
		item.setNews_type_name("腾讯新闻");
		item.setNews_title("冯改朵在我市调研惩罚体系工作");
		item.setRelease_time("2014//12/23");
		item.setRelease_people("浏览 1124 次");
		item.setNews_content_html("http://192.168.1.204:8080/llwif/service/news/E90F089B35224737B9B73654C2A4C5D7.html");*/
//		mNewsList.add(item);
		
		mPullListView = (PullLoadListView) findViewById(R.id.news_item_ListView);
		mPullListView.setCanRefresh(true);
		adapter= new NewsListViewAdapter (this,mNewsList);
		mPullListView.setAdapter(adapter);
		mPullListView.setOnItemClickListener(OnItemClickListener);
		mPullListView.setOnRefreshListener(refreshListener);
	
	}
	
	private OnItemClickListener OnItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			if (--arg2 >= 0) { // -- 下拉刷新bug，少了第一条数据}
				//新闻详情
			Intent intents = new Intent(NotificationActivity.this,GeneralWebviewActivity.class);
			intents.putExtra(GeneralWebviewActivity.URL_KEY, RequestURL.getNewsHtml(mNewsList.get(arg2).getNews_id()));
			startActivity(intents);
			}
		}
	};

	@Override
	protected void initialized() {
		getNews();
	}

	private void getNews() {
		MobileApplication.getInstance().clientTask.executeGet(TaskID.TASK_APP_ZHENGWU_NOTIFICATION, DataFormat.STRING, RequestURL.getNewsList(), this);
	}

	@Override
	protected void threadTask() {
		
	}
	
	@Override
	public void onResponse(Object response, Enum<?> reqId) {
		super.onResponse(response, reqId);
		MobileApplication.cacheUtils.put(TaskID.TASK_APP_ZHENGWU_NOTIFICATION+"",response.toString());
		parseNews(response);
	}

	private void parseNews(Object response) {
		mPullListView.onRefreshComplete();
		Gson gson = new Gson();
		JsonPageModel<NewItem> news = gson.fromJson(response.toString(),new TypeToken<JsonPageModel<NewItem>>(){}.getType() );
			System.out.println("news ="+news);
		mNewsList = news.getResult().getRows();
		adapter.setData(mNewsList);
		adapter.notifyDataSetChanged();
	}
	
	@Override
	public void onErrorResponse(VolleyError error) {
		super.onErrorResponse(error);
		String cacahe = MobileApplication.cacheUtils.getAsString(TaskID.TASK_APP_ZHENGWU_NOTIFICATION+"");
		
		if (!TextUtils.isEmpty(cacahe)) {
			parseNews(cacahe);
		}
	}
	
	private OnRefreshListener refreshListener = new OnRefreshListener() {
		
		@Override
		public void onRefresh() {
			getNews();
			MobileApplication.getInstance().clientTask.cancelLoadingView();
		}
	};
}
